package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.LogicException;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dto.ChannelDto;
import com.heima.model.wemedia.pojo.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.model.wemedia.pojo.WmChannel;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 自媒体频道表(WmChannel)表服务实现类
 *
 * @author dwxw
 * @since 2023-03-09 10:00:52
 */
@Service("wmChannelService")
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    @Resource
    private WmChannelMapper wmChannelMapper;

    @Resource
    private WmNewsMapper wmNewsMapper;

    @Override
    public List<WmChannel> channels() {
        //封装查询条件
        LambdaQueryWrapper<WmChannel> wrapper = Wrappers.<WmChannel>lambdaQuery()
                .eq(WmChannel::getStatus, 1)
                .orderByAsc(WmChannel::getOrd);
        //查询频道列表
        List<WmChannel> channels = wmChannelMapper.selectList(wrapper);

        return channels;
    }

    /**
     * 保存
     *
     * @param wmChannel
     */
    @Override
    public void insert(WmChannel wmChannel) {
        WmChannel channel = wmChannelMapper.selectOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getName, wmChannel.getName()));
        if (channel != null) {
            throw  new LogicException(AppHttpCodeEnum.DATA_EXIST);
        }
        //2.保存
        wmChannel.setCreatedTime(new Date());
        wmChannel.setUpdatedTime(new Date());

        wmChannelMapper.insert(wmChannel);
    }

    /**
     * 查询
     *
     * @param dto
     * @return
     */
    @Override
    public IPage findByNameAndPage(ChannelDto dto) {
        //模糊查询+分页
        IPage page = new Page(dto.getPage(), dto.getSize());

        //频道名称模糊查询
        LambdaQueryWrapper<WmChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())) {
            lambdaQueryWrapper.like(WmChannel::getName, dto.getName());
        }

        return wmChannelMapper.selectPage(page, lambdaQueryWrapper);
    }

    /**
     * 修改
     *
     * @param wmChannel
     */
    @Override
    public void update(WmChannel wmChannel) {
        //判断是否被引用
        long count = wmNewsMapper.selectCount(Wrappers.<WmNews>lambdaQuery().eq(WmNews::getChannelId, wmChannel.getId()).eq(WmNews::getStatus, 9));
        if (count > 0) {
            throw new LogicException(AppHttpCodeEnum.DATA_IS_REFERENCED);
        }
        //2.修改
        wmChannelMapper.updateById(wmChannel);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {
        //2.查询频道
        WmChannel wmChannel = wmChannelMapper.selectById(id);
        if (wmChannel == null) {
            throw new LogicException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //3.频道是否有效
        if (wmChannel.getStatus() == 1) {
            throw new LogicException(AppHttpCodeEnum.DATA_IS_ENABLE);
        }
        //判断是否被引用
        long count = wmNewsMapper.selectCount(Wrappers.<WmNews>lambdaQuery().eq(WmNews::getChannelId, wmChannel.getId()).eq(WmNews::getStatus, 9));
        if (count > 0) {
            throw new LogicException(AppHttpCodeEnum.DATA_IS_REFERENCED);
        }
        //4.删除
        wmChannelMapper.deleteById(id);
    }
}

